package com.lucker.common.util;

import java.io.File;
import java.io.IOException;
import java.net.InetAddress;

import com.lucker.common.model.Local;
import com.lucker.common.model.Province;
import com.maxmind.geoip2.DatabaseReader;
import com.maxmind.geoip2.model.CityResponse;
import com.maxmind.geoip2.record.Location;
import com.maxmind.geoip2.record.Subdivision;

public class LocalUtil {
	
	public static DatabaseReader reader ;
	
	public static DatabaseReader getDatabaseReader(String local_db_path) throws IOException{
		if(reader == null) {
			 File database = new File(local_db_path); 
			 reader = new DatabaseReader.Builder(database).build(); 
			 return reader;
		}
		return reader;
	}
	
	public static Local getLocal(String ip) throws Exception {
		try {
				Local local = new Local();
				InetAddress ipAddress = InetAddress.getByName(ip);  
				if(reader == null) {
					//暂时写死
					reader = getDatabaseReader("/usr/local/lucker/GeoLite2-City.mmdb");
				}
			    CityResponse response = reader.city(ipAddress);
		
			    
			    Subdivision subdivision = response.getMostSpecificSubdivision();
			    Province pro = new Province();
			    pro.setCnName(subdivision.getNames().get("zh-CN"));
			    pro.setEnName(subdivision.getName());
			    pro.setIsoCode(subdivision.getIsoCode());
			    local.setProvince(pro);
			    
			    com.maxmind.geoip2.record.City _city = response.getCity();
			    com.lucker.common.model.City city = new com.lucker.common.model.City();
			    city.setEnName(_city.getName());
			    city.setCnName(_city.getNames().get("zh-CN"));
			    Location location = response.getLocation();
			    city.setLatitude(location.getLatitude());
			    city.setLongitude(location.getLongitude());
			    local.setCity(city);	    
				return local;
		}catch (Exception e) {
			    return null;
		}
	} 
	
	
	
	public static void main(String[] args) throws Exception{
//		  // 创建 GeoLite2 数据库     
//	      File database = new File("d:\\GeoLite2-City.mmdb");     
//	      // 读取数据库内容   
//	      DatabaseReader reader = new DatabaseReader.Builder(database).build();       
//	      InetAddress ipAddress = InetAddress.getByName("171.108.233.157");     
//
//	      // 获取查询结果      
//	      CityResponse response = reader.city(ipAddress);     
//
//	      // 获取国家信息
//	      Country country = response.getCountry();
//	      System.out.println(country.getIsoCode());               // 'CN'
//	      System.out.println(country.getName());                  // 'China'
//	      System.out.println(country.getNames().get("zh-CN"));    // '中国'
//
//	      // 获取省份
//	      Subdivision subdivision = response.getMostSpecificSubdivision();
//	      System.out.println(subdivision.getName());   // 'Guangxi Zhuangzu Zizhiqu'
//	      System.out.println(subdivision.getIsoCode()); // '45'
//	      System.out.println(subdivision.getNames().get("zh-CN")); // '广西壮族自治区'
//
//	      // 获取城市
//	      City city = response.getCity();
//	      System.out.println(city.getName()); // 'Nanning'
//	      Postal postal = response.getPostal();
//	      System.out.println(postal.getCode()); // 'null'
//	      System.out.println(city.getNames().get("zh-CN")); // '南宁'
//	      Location location = response.getLocation();
//	      System.out.println(location.getLatitude());  // 22.8167
//	      System.out.println(location.getLongitude()); // 108.3167
		
		
		String str = "171.108.233.157";
		
		System.out.println(LocalUtil.getLocal(str).getProvince().getCnName() + " ----- " +LocalUtil.getLocal(str).getCity().getCnName());
		
		
		
	}

}
